package com.zhwmall.product.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhwmall.common.entity.product.AttrAttrgroupRelationEntity;
import com.zhwmall.common.entity.product.AttrEntity;
import com.zhwmall.common.utils.PageUtils;
import com.zhwmall.common.utils.Query;
import com.zhwmall.product.dao.AttrAttrgroupRelationDao;
import com.zhwmall.product.dao.AttrDao;
import com.zhwmall.product.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrGroupServiceImpl attrGroupService;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public IPage<AttrEntity> pageList(Map<String, Object> params, long catId) {
        IPage page = new Page(Long.parseLong((String) params.get("limit")), Long.parseLong((String) params.get("page")));
        String input = (String) params.get("input");
        String selectValue = (String) params.get("selectValue");

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(input)){
            wrapper.and((obj)->{
                obj.eq("attr_id", input.trim())
                        .or()
                        .like("attr_name", input.trim())
                        .or()
                        .eq("attr_type", selectValue);

            });
        }
        if (StrUtil.isNotBlank(selectValue)){
            wrapper.and((obj)->{
                obj.eq("attr_type", Integer.valueOf(selectValue));
            });
        }
        if (catId == 0){
            IPage<AttrEntity> iPage = baseMapper.selectPage(page, wrapper);
            List<AttrEntity> entities = iPage.getRecords();
            entities.forEach((item)->{
                List<String> list =  CollUtil.newArrayList();

                List<String> names = attrGroupService.findCateLogNamesByCatId(item.getCatelogId(), list);

                List<String> reverse = CollectionUtil.reverse(names);

                item.setCatelogNames(reverse.toArray(new String[reverse.size()]));

            });
            return iPage;
        }else {
            wrapper.eq("catelog_id", catId);
            return baseMapper.selectPage(page, wrapper);
        }
    }

    @Override
    public int removeAttrByIds(List<Long> asList) {

        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationDao.selectBatchIds(asList);
        if (CollUtil.isNotEmpty(list)){
            return 0;
        }
        int ids = baseMapper.deleteBatchIds(asList);

        return ids;
    }

    @Override
    public PageUtils getSaleAttr(Map<String, Object> params, long catId) {

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", catId).eq("attr_type", 0);
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

}